package com.jcfk.system.service;

import com.jcfk.system.domain.po.SysNoticeReadRecord;
import com.jcfk.system.domain.query.SysNoticeReadRecordQuery;
import com.jcfk.system.domain.dto.SysNoticeReadRecordDTO;
import com.jcfk.system.dao.SysNoticeReadRecordDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 通知阅读记录
 *
 * @author yrl
 * @email
 * @date 2024-12-06
 */
@Service
public class SysNoticeReadRecordService extends BaseService<SysNoticeReadRecordDao, SysNoticeReadRecord> {

    /**
     * SysNoticeReadRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<SysNoticeReadRecordDTO> queryByPage(DataPage<SysNoticeReadRecordDTO> page, SysNoticeReadRecordQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断SysNoticeReadRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(SysNoticeReadRecordDTO dto) {
        QueryWrapper<SysNoticeReadRecord> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入通知阅读记录
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysNoticeReadRecordDTO dto) {

        // 保存
        SysNoticeReadRecord item = new SysNoticeReadRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新通知阅读记录
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SysNoticeReadRecordDTO dto) {

        // 保存
        SysNoticeReadRecord item = new SysNoticeReadRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(SysNoticeReadRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取通知阅读记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public SysNoticeReadRecordDTO get(String id) {
        // 查询数据
        SysNoticeReadRecord item = this.baseMapper.selectById(id);
        SysNoticeReadRecordDTO dto = new SysNoticeReadRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SysNoticeReadRecordDTO>
     */
    public List<SysNoticeReadRecordDTO> getList(SysNoticeReadRecordQuery query) {
        // 查询条件
        QueryWrapper<SysNoticeReadRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getNoticeId()), SysNoticeReadRecord::getNoticeId, query.getNoticeId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getReaderId()), SysNoticeReadRecord::getReaderId, query.getReaderId());
        return this.baseMapper.queryList(qw);
    }

    /**
     * 根据主键获取通知阅读记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }
}

